package com.example.LightningController;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: master
 * Date: 6/14/13
 * Time: 11:26 PM
 * To change this template use File | Settings | File Templates.
 */
public class EditZone extends Activity {
    private DatabaseManager databaseManager;
    private ArrayList<Zone> zones;
    private Zone zone;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.edit_zone);

        databaseManager = new DatabaseManager(this);
        zones = new ArrayList<Zone>();

        zones = getIntent().getParcelableArrayListExtra("all_available_zones");
        Bundle b = getIntent().getExtras();
        zone = (Zone) b.get("selected_zone");

        ((EditText) findViewById(R.id.zone_name_edit_text)).setText(zone.getTag());
        ((EditText) findViewById(R.id.zone_description_edit_text)).setText(databaseManager.retrieveZonesDescription(String.valueOf(databaseManager.GetRecordID(zone.getTag(), zone.getMacAddress()))));
        ((TextView) findViewById(R.id.zone_mac_edit_text)).setText(zone.getMacAddress());
    }

    public void deleteSelectedZone(View v) {
        Utility.showProgressDialog(this, "Removing device");
        new Thread(new Runnable() {
            @Override
            public void run() {
                //To change body of implemented methods use File | Settings | File Templates.
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                Intent returnIntent = new Intent();
                setResult(RESULT_OK, returnIntent);
                finish();
            }
        }).start();
    }

    public void cancelEdit(View v) {
        Intent returnIntent = new Intent();
        setResult(RESULT_CANCELED, returnIntent);
        finish();
    }

    public void saveChanges(View v) {
        Utility.showProgressDialog(this, "Updating settings");
        final Thread saveThread = new Thread(new Runnable() {
            @Override
            public void run() {
                //To change body of implemented methods use File | Settings | File Templates.
                int i = databaseManager.updateRecordInDatabase(String.valueOf(databaseManager.GetRecordID(zone.getTag(), zone.getMacAddress())), ((EditText) findViewById(R.id.zone_name_edit_text)).getText().toString(), zone.getMacAddress(), ((EditText) findViewById(R.id.zone_description_edit_text)).getText().toString());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                if (i > 0) {
                    Intent returnIntent = new Intent();
                    setResult(RESULT_CANCELED, returnIntent);
                    finish();
                    Utility.stopProgressDialog();
                } else {
                    Utility.stopProgressDialog();
                    Utility.showAlertDialog(EditZone.this, "Couldn't update settings", "Oops");
                }
            }
        });

        saveThread.start();
    }
}
